** Organize data in a format that is ready for parent-level analysis, for
** parents in the middle generation.  Includes calculation of max EITC
** amount each middle-generation parent could be eligible for, based on 
** that person's number of kids and state of residence.

** Also extract some information that will eventually be merged in with
** grandparent files:
**		The maximum EITC amount for any mid-gen parent in the HH (in a given month)
**		Whether the middle generation includes a single mom? A single mom with no college?
*******************************************************************************

use CPS/MidGenParentData.dta 

** Merge in counts of EITC-eligible own kids
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 serial year pernum using  CPS/MomKidCount_temp
rename _merge _merge_mom
merge 1:1 serial year pernum using  DadKidCount_temp, update replace
** There are KidCount values for some people not identified as PotentialMidGenParent people
** Drop those cases
keep if PotentialMidGenParent==1
drop _merge _merge_mom


gen EITC_calc_year=(year)-1
rename statefip tfipsst
gen xocah = DepCount
quietly do EITC_Calculator.do 


** Now merge in simulated EITC amount (average for cell defined
** in terms of YearOfTaxCalc, tfipsst, Married, KidsForSimulated
gen YearOfTaxCalc = (year)-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
keep if _merge==3
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename tfipsst statefip
rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep

gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort serial year month 
by serial year month: egen N_SingleMom=total(SingleMom) 
by serial year month: egen N_SingleNoCollegeMom=total(SingleNoCollegeMom) 
by serial year month: egen N_ParsWithCollege=total(temp_college) 
by serial year month: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad) 
by serial year month: egen N_ParsEmployed=total(MidGenPar_Employed) 
by serial year month: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep) 


gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0) 
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0) 
gen NoParsWithCollege = (N_ParsWithCollege==0) 
gen NoCollegeGradPars = (N_CollegeGradPars==0) 
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(relate==101) 
sort serial year month 
by serial year month: egen N_RefPerson=total(RefPerson) 
gen MidGenHasRefPerson=(N_RefPerson>0) 

save CPS/OrganizedMidGenParentData, replace




** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.
** Also extract info about whether the middle generation includes any single mom,
** or any single mom with no college education, any employed parents, a HH ref person, etc
keep serial year month r_MaxEITC_thou r_ref_MaxEITC r_MaxFedEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson  AnyParsPotentialDeps

sort serial year month
by serial year month: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by serial year month: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by serial year month: egen r_MaxFedEITC_MidGenMax = max(r_MaxFedEITC_thou)
by serial year month: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah* r_MaxEITC_thou r_ref_MaxEITC r_MaxFedEITC_thou taxsim_eic_thou
duplicates drop

duplicates report serial year month

save CPS/MaxMidGenEITC, replace
clear


** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData

keep serial year month r_MaxEITC_thou r_ref_MaxEITC r_MaxFedEITC_thou taxsim_eic_thou PotentialMidGenMom
keep if PotentialMidGenMom==1
duplicates report serial year month
sort serial year month
by serial year month: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by serial year month: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by serial year month: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by serial year month: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
keep serial year month Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms
duplicates drop
save CPS/SumMidGenEITC, replace
clear


*******************************************************

use CPS/OrganizedMidGenParentData
merge m:1 serial year month using CPS/SumMidGenEITC
drop _merge

merge m:1 serial year month using CPS/MaxMidGenEITC

** NOTE: You don't drop people with missing self-reported health status in SIPP
** analysis, but that's because you might still want to include them in financial
** regressions.  Here you are only doing health regressions, thus can drop 
** people with missing value of health variable
drop if health==.
** NOTE: CPS only begins collecting self-reported health status in 1996, so this
** drops all observations from earlier years.
gen ExcellentVeryGoodHealth = (health==1 | health==2)
gen PoorHealth = (health==5)

gen AgeSq = age*age

gen tfipsst=statefip
gen rhcalyr = year

drop _merge
merge m:1 tfipsst rhcalyr using OrganizedStateControls
tab _merge




keep if _merge==3
drop _merge

gen r_MaxWelf = MaxWelf*real



save CPS/OrganizedParentLevelData, replace

